package com.ruicar.afs.cloud.risk.tortoise.processor.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.risk.api.dto.tortoise.TortoiseQueryBean;
import com.ruicar.afs.cloud.risk.api.enums.tortoise.GwtTortoiseStartApproveType;
import com.ruicar.afs.cloud.risk.tortoise.components.GwtTortoiseStrRedis;
import com.ruicar.afs.cloud.risk.tortoise.config.GwtTortoiseCallbackUriConfig;
import com.ruicar.afs.cloud.risk.tortoise.consts.GwtTortoiseConst;
import com.ruicar.afs.cloud.risk.tortoise.dto.request.GwtTortoiseStartRequest;
import com.ruicar.afs.cloud.risk.tortoise.dto.response.GwtTortoiseStartResponse;
import com.ruicar.afs.cloud.risk.tortoise.enums.*;
import com.ruicar.afs.cloud.risk.tortoise.feign.GwtTortoiseFeign;
import com.ruicar.afs.cloud.risk.tortoise.processor.AfsTortoiseProcessor;
import com.ruicar.afs.cloud.risk.tortoise.service.AfsCreditTortoiseRecordService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * @Description
 * @Author Peng Jinbo
 * @Date 2020/07/27 18:09
 */
@Data
@AllArgsConstructor
@Service
@Slf4j
public class AfsTortoiseProcessorImpl implements AfsTortoiseProcessor {

    /**
     * Callback uri config
     */
    private GwtTortoiseCallbackUriConfig callbackUriConfig;

    /**
     * Gwt str redis
     */
    private final GwtTortoiseStrRedis gwtStrRedis;

    /**
     * String redis template
     */
    private final StringRedisTemplate stringRedisTemplate;
    /**
     * Gwt tortoise feign
     */
    private final GwtTortoiseFeign gwtTortoiseFeign;

    /**
     * Credit query record service
     */
    private AfsCreditTortoiseRecordService creditQueryRecordService;

    /**
     * Author Peng Jinbo
     * Apply *
     *
     * @param queryBean query credit business data
     */
    @Override
    public IResponse<String> signal(TortoiseQueryBean queryBean) {
        GwtTortoiseStartRequest startRequest = GwtTortoiseStartRequest.builder()
                .applicationNo(queryBean.getApplyNo())
                .caMainId(queryBean.getMainId())
                .callBackUrl(callbackUriConfig.getCallbackUri() + GwtTortoiseConst.SYSTEM_CLIENT_PREFIX + GwtTortoiseConst.TORTOISE_CALLBACK_URI)
                .approveType(Objects.nonNull(queryBean.getApproveType()) ? queryBean.getApproveType() : GwtTortoiseStartApproveType.NORMAL)
                .sceneCode(queryBean.getScene())
                .callStep(queryBean.getCallStep())
                .sysCallLogId(queryBean.getSerialNumber())
                .channelType(GwtTortoiseChannelType.MULTI_SYSTEM)
                .build();
        try {
            ObjectMapper o = new ObjectMapper();
            log.info("发送玄武信号 : {}",o.writeValueAsString(startRequest));
        }catch (Exception e){}
        GwtTortoiseStartResponse response = gwtTortoiseFeign.start(startRequest);
        gwtStrRedis.set(queryBean);
        log.info("玄武信号响应: {}", response);
        if(GwtTortoiseConst.TORTOISE_RESPONSE_SUCCESS.equals(response.getErrorCode())) {
            creditQueryRecordService.saveRecord(queryBean,response.getData().getTrackingId());
            return IResponse.success(response.getData().getTrackingId());
        }else{
            return IResponse.fail(response.getErrorMsg());
        }
    }

}
